<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <ProcessTaskDetailDrawer @register="registerDetailDrawer" @success="handleSuccess" type="done" />
  </div>
</template>

<script lang="ts" name="system-position" setup>
  import { ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useDrawer } from '/@/components/Drawer';
  import { todoList } from './processtask.api';
  import { doneColumns, doneSearchFormSchema } from './processtask.data';
  import ProcessTaskDetailDrawer from './ProcessTaskDetailDrawer.vue';

  // 注册办理 drawer
  const [registerDetailDrawer, { openDrawer: openDetailDrawer }] = useDrawer();

  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    designScope: 'processTask-todo-template',
    tableProps: {
      title: '我的已办列表',
      api: todoList,
      columns: doneColumns,
      formConfig: {
        schemas: doneSearchFormSchema,
      },
      actionColumn: {
        width: 200,
      },
      showIndexColumn: true,
      beforeFetch: handleBeforeFetch,
    },
  });

  const [registerTable, { reload, setLoading, clearSelectedRowKeys }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: '详情',
        onClick: handleExecute.bind(null, record),
        auth: ['admin', 'wf:processTask:doneDetail'], // 根据权限控制是否显示: 无权限，不显示
      },
    ];
  }

  // 处理查询请求参数
  function handleBeforeFetch(params) {
    if (params) {
      Object.keys(params).forEach((key) => {
        if (params[key] == 9999999999) {
          // 删除全部自定义的值-不提交到后端
          delete params[key];
        }
      });
    }
    return params;
  }

  // 处理流程
  function handleExecute(record) {
    openDetailDrawer(true, {
      record,
    });
  }

  // 提交成功刷新
  function handleSuccess() {
    reload();
  }
</script>

<style lang="less" scoped></style>
